package com.fjec.market.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import java.io.UnsupportedEncodingException;

@Slf4j
public class SubmitPhyOrderCallback implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {

    public static SubmitPhyOrderCallback INSTANCE = new SubmitPhyOrderCallback();

    private SubmitPhyOrderCallback() {

    }

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if (!ack) {
            log.error("correlationData：" + correlationData);
            log.error("ack：" + ack);
            log.error("cause：" + cause);
        }
    }

    @Override
    public void returnedMessage(Message message, int replyCode, String replyText,
                                String exchange, String routingKey) {
        //失败的回调
        try {
            System.out.println("ReturnCallback:     " + "消息："
                    + new String(message.getBody(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println("ReturnCallback:     " + "回应码：" + replyCode);
        System.out.println("ReturnCallback:     " + "回应信息：" + replyText);
        System.out.println("ReturnCallback:     " + "交换机：" + exchange);
        System.out.println("ReturnCallback:     " + "路由键：" + routingKey);
    }
}
